'use strict';

angular.module('com.dcos.app').controller('ProjectCenterController', function($scope,$uibModal,$window,api,$state,ProjectCenterService) {
  var vm = this;

  vm.initPageSize = api.initPageSize;

  vm.projectData=[];

  vm.msg="删除项目会同步删除项目下的所有流水线，确定要删除该项目？";

  vm.userid=$window.sessionStorage.getItem("curUserId");
  vm.currentUser = $window.sessionStorage.getItem("curUserName");

  // if(!vm.userid)
  //   $state.go('login');
  
  vm.getProjectList = function () {
    vm.loading = false;
    
    ProjectCenterService.getlist(vm.userid).then(function (response) {
      vm.projectData=response.data.projectlist;
      $window.sessionStorage.setItem("projectList",angular.toJson(vm.projectData,true));
      vm.page = 1;
      angular.forEach(vm.projectData,function(item,index){
        item.success_rate=(item.success_rate*100).toFixed(2).toString();
      });

      vm.pageSize = vm.initPageSize;
      vm.totalCount = vm.projectData.length;
      vm.reverse = false;
    });
  }
  vm.getProjectList();
  //创建
  vm.create = function()
  {
    var modalInstance = $uibModal.open({
      animation: true,
      templateUrl: 'controllers/projectcenter/detail/detail.html',
      controller: 'ProjectDetailController as vm',
      resolve: {
        type:function(){return "create";},
        project:function(){return null;},
        reload:function(){return vm.getProjectList;}
      }
    });
  }
  //编辑
  vm.edit = function(project)
  {
    var modalInstance = $uibModal.open({
      animation: true,
      templateUrl: 'controllers/projectcenter/detail/detail.html',
      controller: 'ProjectDetailController as vm',
      resolve: {
        type:function(){return "edit";},
        project:function(){return project;},
        reload:function(){return vm.getProjectList;}
      }
    });
  }
  vm.view = function(project_id) {
    $window.sessionStorage.setItem("curProjectId",project_id);
    $state.go('app.dashboard');
  }
  $scope.ok = function(id) {
    ProjectCenterService.delete(id).then(function (response) {
      vm.getProjectList();
    });
  }
  vm.logout = function () {
      $window.sessionStorage.clear();
      $state.go("login");
  };
  $(function(){
    $(".marquee>div").width($("span").width());
  });
});
